/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;

import org.apache.commons.io.FileUtils;

import com.thienloc.omap.entity.report.MoStatistic;
import com.thienloc.omap.jdbc.dao.RawMoDao;
import com.thienloc.omap.session.TblRawMoFacade;
import com.thienloc.omap.util.Constant;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class TblRawMoBean extends BaseBean implements Serializable {

    @EJB
    private TblRawMoFacade facade;
    private Set<String> mdnList;
    private String syntaxString;
    private String days;
    private List<String> selectedShortCodes;
    private List<MoStatistic> moStatistics;
    private Map<String, String> shortCodeMap;
    private List<String> selectedDates;
    private String mdnListString;
    private final Logger logger = LoggerFactory.getLogger(TblRawMoBean.class);

    @PostConstruct
    public void init() {
        try {
            shortCodeMap = new TreeMap<String, String>();
            File file = new File(Constant.CONFIG_FOLDER + "shortCodeList.txt");
            List<String> shortCodes = FileUtils.readLines(file);
            for (String shortCode : shortCodes) {
                shortCodeMap.put(shortCode, shortCode);
            }
        } catch (IOException ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public void findContentStatistic() {
        try {
            List<String> syntaxes = Arrays.asList(syntaxString.split("\\r\\n"));
            String[] dayList = days.split(",");
            selectedDates = Arrays.asList(dayList);
//            selectedDates = new ArrayList<Date>(dayList.length);
//            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//            for (String day : dayList) {
//                selectedDates.add(sdf.parse(day));
//            }
            RawMoDao rawMoDao = new RawMoDao();
            moStatistics = rawMoDao.findByContentList(syntaxes, selectedShortCodes, selectedDates);
            for (MoStatistic moStat : moStatistics) {
                Set<String> msisdnList = rawMoDao.findSubscriberList(moStat.getContent(), moStat.getShortCode(), selectedDates);
                moStat.setCount(BigDecimal.valueOf(msisdnList.size()));
            }
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public void findSubscriberList(String content, String shortCode) {
        try {
            RawMoDao rawMoDao = new RawMoDao();
            mdnList = rawMoDao.findSubscriberList(content, shortCode, selectedDates);
            StringBuilder sb = new StringBuilder();
            for (String mdn : mdnList) {
                sb.append(mdn).append('\n');
            }
            mdnListString = sb.toString();
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public void search(String allContent, Date date, String shortCode) {
        try {
            List<String> contents = Arrays.asList(allContent.split(","));
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            Date start = cal.getTime();

            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            Date end = cal.getTime();

            mdnList = facade.findMdnByContentList(contents, start, end, shortCode);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public String getMdnListString() {
        return mdnListString;
    }

    public Set<String> getMdnList() {
        return mdnList;
    }

    public void setMdnList(Set<String> mdnList) {
        this.mdnList = mdnList;
    }

    public String getSyntaxString() {
        return syntaxString;
    }

    public void setSyntaxString(String syntaxString) {
        this.syntaxString = syntaxString;
    }

    public String getDays() {
        return days;
    }

    public void setDays(String days) {
        this.days = days;
    }

    public List<String> getSelectedShortCodes() {
        return selectedShortCodes;
    }

    public void setSelectedShortCodes(List<String> selectedShortCodes) {
        this.selectedShortCodes = selectedShortCodes;
    }

    public Map<String, String> getShortCodeMap() {
        return shortCodeMap;
    }

    public void setShortCodeMap(Map<String, String> shortCodeMap) {
        this.shortCodeMap = shortCodeMap;
    }

    public List<MoStatistic> getMoStatistics() {
        return moStatistics;
    }

    public void setMoStatistics(List<MoStatistic> moStatistics) {
        this.moStatistics = moStatistics;
    }
    private Date startDate;
    private Date endDate;
    private String dailyStartTime;
    private String dailyEndTime;
    private List<String> selectedServiceCodes;
    private List<String> selectedOperators;
    private Set<String> list = new TreeSet<String>();

    public void findRawMo() {
        try {
            RawMoDao rawMoDao = new RawMoDao();
            list = rawMoDao.getRawMoList(startDate, endDate, dailyStartTime, dailyEndTime, selectedServiceCodes, selectedOperators);
            if (list.isEmpty()) {
                JsfUtil.addMessage(FacesMessage.SEVERITY_WARN, "Kết quả rỗng", "Không có thuê bao nào thỏa mãn yêu cầu tìm kiếm");
            }
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public List<String> getSelectedOperators() {
        return selectedOperators;
    }

    public void setSelectedOperators(List<String> selectedOperators) {
        this.selectedOperators = selectedOperators;
    }

    public List<String> getSelectedServiceCodes() {
        return selectedServiceCodes;
    }

    public void setSelectedServiceCodes(List<String> selectedServiceCodes) {
        this.selectedServiceCodes = selectedServiceCodes;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Set<String> getList() {
        return list;
    }

    public void setList(Set<String> list) {
        this.list = list;
    }

    public String getDailyEndTime() {
        return dailyEndTime;
    }

    public void setDailyEndTime(String dailyEndTime) {
        this.dailyEndTime = dailyEndTime;
    }

    public String getDailyStartTime() {
        return dailyStartTime;
    }

    public void setDailyStartTime(String dailyStartTime) {
        this.dailyStartTime = dailyStartTime;
    }

    public String getListStr() {
        StringBuilder sb = new StringBuilder();
        for (String mdn : list) {
            sb.append(mdn);
            sb.append("\r\n");
        }
        return sb.toString();
    }
}
